package org.black.phoneUtility.sms.reply.app.fragment;

import java.util.Calendar;
import java.util.Date;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.database.dataModel.SmsReply;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class SmsReplyReplyDateTimeRepeatDialogFragment extends DialogFragment
		implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
	public static final String TAG = SmsReplyReplyDateTimeRepeatDialogFragment.class
			.getName();
	public static final String ARGUMENTS_REPLY_DATE_TIME_REPEAT = "replyDateTimeRepeat";

	public interface SmsReplyReplyDateTimeRepeatListener {
		public void onReplyDateTimeRepeatDialogPositiveClick(
				SmsReply replyDateTimeModeRepeat);

		public void onReplyDateTimeRepeatDialogNegativeClick();
	}

	private SmsReplyReplyDateTimeRepeatListener smsReplyReplyDateTimeRepeatListener;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyReplyDateTimeRepeatListener = (SmsReplyReplyDateTimeRepeatListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement SmsReplyReplyDateTimeRepeatListener");
		}
	}

	private SmsReply replyDateTimeModeRepeat;

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		replyDateTimeModeRepeat = (SmsReply) getArguments().getSerializable(
				ARGUMENTS_REPLY_DATE_TIME_REPEAT);
		View view = inflater.inflate(
				R.layout.sms_reply_reply_date_time_repeat_dialog, null);
		Button smsReplyDateTimeRepeatStartTimeButton = (Button) view
				.findViewById(R.id.smsReplyDateTimeRepeatStartTimeButton);
		smsReplyDateTimeRepeatStartTimeButton.setText(DateFormat.getTimeFormat(
				getActivity()).format(
				replyDateTimeModeRepeat.getRepeatStartTime()));
		smsReplyDateTimeRepeatStartTimeButton.setOnClickListener(this);
		Button smsReplyDateTimeRepeatEndTimeButton = (Button) view
				.findViewById(R.id.smsReplyDateTimeRepeatEndTimeButton);
		smsReplyDateTimeRepeatEndTimeButton.setText(DateFormat.getTimeFormat(
				getActivity()).format(
				replyDateTimeModeRepeat.getRepeatEndTime()));
		smsReplyDateTimeRepeatEndTimeButton.setOnClickListener(this);

		ToggleButton smsReplyDateTimeRepeatSundayToggleButton = (ToggleButton) view
				.findViewById(R.id.smsReplyDateTimeRepeatSundayToggleButton);
		smsReplyDateTimeRepeatSundayToggleButton
				.setChecked(replyDateTimeModeRepeat.isSundayEnable());
		smsReplyDateTimeRepeatSundayToggleButton
				.setOnCheckedChangeListener(this);
		ToggleButton smsReplyDateTimeRepeatMondayToggleButton = (ToggleButton) view
				.findViewById(R.id.smsReplyDateTimeRepeatMondayToggleButton);
		smsReplyDateTimeRepeatMondayToggleButton
				.setChecked(replyDateTimeModeRepeat.isMondayEnable());
		smsReplyDateTimeRepeatMondayToggleButton
				.setOnCheckedChangeListener(this);
		ToggleButton smsReplyDateTimeRepeatTuesdayToggleButton = (ToggleButton) view
				.findViewById(R.id.smsReplyDateTimeRepeatTuesdayToggleButton);
		smsReplyDateTimeRepeatTuesdayToggleButton
				.setChecked(replyDateTimeModeRepeat.isTuesdayEnable());
		smsReplyDateTimeRepeatTuesdayToggleButton
				.setOnCheckedChangeListener(this);
		ToggleButton smsReplyDateTimeRepeatWednesdayToggleButton = (ToggleButton) view
				.findViewById(R.id.smsReplyDateTimeRepeatWednesdayToggleButton);
		smsReplyDateTimeRepeatWednesdayToggleButton
				.setChecked(replyDateTimeModeRepeat.isWednesdayEnable());
		smsReplyDateTimeRepeatWednesdayToggleButton
				.setOnCheckedChangeListener(this);
		ToggleButton smsReplyDateTimeRepeatThursdayToggleButton = (ToggleButton) view
				.findViewById(R.id.smsReplyDateTimeRepeatThursdayToggleButton);
		smsReplyDateTimeRepeatThursdayToggleButton
				.setChecked(replyDateTimeModeRepeat.isThursdayEnable());
		smsReplyDateTimeRepeatThursdayToggleButton
				.setOnCheckedChangeListener(this);
		ToggleButton smsReplyDateTimeRepeatFridayToggleButton = (ToggleButton) view
				.findViewById(R.id.smsReplyDateTimeRepeatFridayToggleButton);
		smsReplyDateTimeRepeatFridayToggleButton
				.setChecked(replyDateTimeModeRepeat.isFridayEnable());
		smsReplyDateTimeRepeatFridayToggleButton
				.setOnCheckedChangeListener(this);
		ToggleButton smsReplyDateTimeRepeatSaturdayToggleButton = (ToggleButton) view
				.findViewById(R.id.smsReplyDateTimeRepeatSaturdayToggleButton);
		smsReplyDateTimeRepeatSaturdayToggleButton
				.setChecked(replyDateTimeModeRepeat.isSaturdayEnable());
		smsReplyDateTimeRepeatSaturdayToggleButton
				.setOnCheckedChangeListener(this);

		builder.setPositiveButton(android.R.string.ok, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				replyDateTimeModeRepeat
						.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_REPEAT_TYPE);
				smsReplyReplyDateTimeRepeatListener
						.onReplyDateTimeRepeatDialogPositiveClick(replyDateTimeModeRepeat);

			}
		}).setNegativeButton(android.R.string.cancel, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.setView(view);

		builder.setTitle(R.string.sms_reply_add_edit_date_time_repeat);
		return builder.create();
	}

	@Override
	public void onClick(View v) {
		SmsReplyReplyDateTimeRepeatTimePickerDialogFragment smsReplyReplyDateTimeRepeatTimePickerDialogFragment = new SmsReplyReplyDateTimeRepeatTimePickerDialogFragment();
		Bundle bundle = new Bundle();
		int hourOfDay, minute;
		boolean isBegin;

		if (v.getId() == R.id.smsReplyDateTimeRepeatStartTimeButton) {
			isBegin = true;
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(replyDateTimeModeRepeat
					.getRepeatStartTime().getTime());
			hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
			minute = calendar.get(Calendar.MINUTE);
		} else {
			isBegin = false;
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(replyDateTimeModeRepeat.getRepeatEndTime()
					.getTime());
			hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
			minute = calendar.get(Calendar.MINUTE);
		}
		bundle.putBoolean(
				SmsReplyReplyDateTimeRepeatTimePickerDialogFragment.ARGUMENTS_IS_FROM,
				isBegin);
		bundle.putInt(
				SmsReplyReplyDateTimeRepeatTimePickerDialogFragment.ARGUMENTS_HOUR_OF_DAY,
				hourOfDay);
		bundle.putInt(
				SmsReplyReplyDateTimeRepeatTimePickerDialogFragment.ARGUMENTS_MINUTE,
				minute);
		smsReplyReplyDateTimeRepeatTimePickerDialogFragment
				.setArguments(bundle);
		smsReplyReplyDateTimeRepeatTimePickerDialogFragment.show(
				getFragmentManager(),
				SmsReplyReplyDateTimeRepeatTimePickerDialogFragment.TAG);

	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (buttonView.getId() == R.id.smsReplyDateTimeRepeatSundayToggleButton) {
			replyDateTimeModeRepeat.setSundayEnable(isChecked);
		} else if (buttonView.getId() == R.id.smsReplyDateTimeRepeatMondayToggleButton) {
			replyDateTimeModeRepeat.setMondayEnable(isChecked);
		} else if (buttonView.getId() == R.id.smsReplyDateTimeRepeatTuesdayToggleButton) {
			replyDateTimeModeRepeat.setTuesdayEnable(isChecked);
		} else if (buttonView.getId() == R.id.smsReplyDateTimeRepeatWednesdayToggleButton) {
			replyDateTimeModeRepeat.setWednesdayEnable(isChecked);
		} else if (buttonView.getId() == R.id.smsReplyDateTimeRepeatThursdayToggleButton) {
			replyDateTimeModeRepeat.setThursdayEnable(isChecked);
		} else if (buttonView.getId() == R.id.smsReplyDateTimeRepeatFridayToggleButton) {
			replyDateTimeModeRepeat.setFridayEnable(isChecked);
		} else if (buttonView.getId() == R.id.smsReplyDateTimeRepeatSaturdayToggleButton) {
			replyDateTimeModeRepeat.setSaturdayEnable(isChecked);
		}
	}

	public void handleTimePickerSelection(boolean isBegin, int hourOfDay,
			int minute) {
		Date date = null;
		if (isBegin == true) {
			date = replyDateTimeModeRepeat.getRepeatStartTime();
		} else {
			date = replyDateTimeModeRepeat.getRepeatEndTime();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(date.getTime());
		calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
		calendar.set(Calendar.MINUTE, minute);
		if (isBegin == true) {
			replyDateTimeModeRepeat.setRepeatStartTime(calendar.getTime());

			((Button) getDialog().findViewById(
					R.id.smsReplyDateTimeRepeatStartTimeButton))
					.setText(DateFormat.getTimeFormat(getActivity()).format(
							replyDateTimeModeRepeat.getRepeatStartTime()));
		} else {
			replyDateTimeModeRepeat.setRepeatEndTime(calendar.getTime());
			((Button) getDialog().findViewById(
					R.id.smsReplyDateTimeRepeatEndTimeButton))
					.setText(DateFormat.getTimeFormat(getActivity()).format(
							replyDateTimeModeRepeat.getRepeatEndTime()));
		}

	}
}
